﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Sirenix.Utilities;
using UnityEditor;
using UnityEngine;

[GlobalConfig("StoryEditor/Nodes/Audio")]
public class AudioCtrl : GlobalConfig<AudioCtrl>
{
    public List<AudioClip> Clips;

    // 检索工程音频
    public void AutoSeach()
    {
        string path = Application.dataPath + "/StoryEditor/Audio/";
        var files = Directory.GetFiles(path, "**.**", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".mp3") || s.EndsWith(".ogg") || s.EndsWith(".wav")).ToArray();

        Clips.Clear();
        for (int i = 0; i < files.Length; i++)
        {
            var file = files[i];
            var tmp = new Uri(file).Segments;
            var relative = new StringBuilder();
            bool isStart = false;
            for (int j = 0; j < tmp.Length - 1; j++)
            {
                if (tmp[j].Equals("Assets/"))
                    isStart = true;
                if (isStart)
                    relative.Append(tmp[j]);
            }

            relative.Append(Path.GetFileName(file));
            var clip = AssetDatabase.LoadAssetAtPath<AudioClip>(relative.ToString());
            Clips.Add(clip);
        }
    }
}
